package com.briup.common.config;

import com.briup.common.properties.SwaggerConfigProperties;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import io.swagger.annotations.Api;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 自动配置类-swagger配置
 */
@ConditionalOnProperty("briup.config.swagger.title")
@EnableSwagger2
@Configuration
@Data
public class SwaggerAutoConfig {

    @Autowired
    private SwaggerConfigProperties swaggerConfigProperties;

    @Bean
    public Docket createRestApi() {


        //swagger中要忽略的参数类型
//        String[] str = {
//                "java.security.Principal",
//                "javax.servlet.http.HttpServletResponse",
//                "javax.servlet.http.HttpServletRequest"
//        };
//        List<Class<?>> list = new ArrayList<>();
//
//        for (int i = 0; i < str.length; i++) {
//            try {
//                list.add(Class.forName(str[i]));
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            }
//        }

    	
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
//                .apis(RequestHandlerSelectors.basePackage(basePackage))
                //Controller上加Api注解才能被swagger扫描到
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(security())
                .securityContexts(securityContexts());
//                .ignoredParameterTypes(list.toArray(new Class<?>[list.size()]));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerConfigProperties.getTitle())
                .description(swaggerConfigProperties.getDescription())
                .termsOfServiceUrl(swaggerConfigProperties.getUrl())
                .version(swaggerConfigProperties.getVersion())
                .build();
    }

    private List<ApiKey> security() {
        return Collections.singletonList(
                new ApiKey("Authorization", "Authorization", "header")
        );
    }

    private List<SecurityContext> securityContexts() {
        return Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(antPathsCondition(Arrays.asList(swaggerConfigProperties.getAuthPath())))
                        .build()
        );
    }

    private Predicate<String> antPathsCondition(List<String> antPaths){

        if(antPaths==null||antPaths.size()==0) {
            antPaths = new ArrayList<>();
            antPaths.add("/**");
        }

        List<Predicate<String>> list = new ArrayList<>();

        antPaths.forEach(path->list.add(PathSelectors.ant(path)));

        return Predicates.or(list);

    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Collections.singletonList(
                new SecurityReference("Authorization", authorizationScopes));
    }

}
